#include <gtest/gtest.h>

#include "article_comment/comment_redis_adapter.h"

using namespace std;
using namespace jztzp;

TEST(CommentAdapter, add) {
  RedisAdapter adapter;
  // CommentRemove(int64_t id, int32_t article_id, int32_t sender_id);

  int article_id = 10000007;
  int sender_id = 403201989;
  string content("技术是有趣的，做快乐的事情，挣钱何必那么多呢？");

  jztzp::CommentItem item;
  item.__set_id(100);
  item.__set_state(0x02);
  item.__set_article_id(article_id);
  item.__set_sender_id(sender_id);
  item.__set_sender_name("热爱生命");
  item.__set_time_stamp(time(NULL));
  item.__set_sender_ip("127.0.0.1");
  item.__set_content(content);
  
  size_t count = 5;

  for(size_t i = 0; i < count; ++i) {
    item.__set_id(100 + i);
    EXPECT_TRUE(adapter.CommentAdd(item) == 0);
  }

  CommentList comments;
  adapter.CommentGetByArticle(&comments, article_id, 0, count);
  EXPECT_TRUE(comments.size() == count);
  for(size_t i = 0; i < count; ++i) {
    EXPECT_TRUE(comments[i].content == content);
  }

  comments.clear();
  adapter.CommentGetByArticle(&comments, article_id, 0, 3);
  EXPECT_TRUE(comments.size() == 3);
  for(size_t i = 0; i < 3; ++i) {
    EXPECT_TRUE(comments[i].content == content);
    EXPECT_EQ(adapter.CommentCountByArticle(article_id), count);
  }

  comments.clear();
  adapter.CommentGetByArticle(&comments, article_id, 0, count + 10);
  EXPECT_TRUE(comments.size() == count);
  for(size_t i = 0; i < count; ++i) {
    EXPECT_TRUE(comments[i].content == content);
  }

  for(size_t i = 0; i < count; ++i) {
    EXPECT_TRUE(adapter.CommentRemove(100 + i, article_id, sender_id) == 0);
    comments.clear();
    adapter.CommentGetByArticle(&comments, article_id, 0, count);
    EXPECT_EQ(comments.size(), count - 1 - i);
    EXPECT_EQ(adapter.CommentCountByArticle(article_id), count - 1 - i);
  }

  comments.clear();
  adapter.CommentGetByArticle(&comments, article_id, 0, count);
  EXPECT_EQ(comments.size(), 0);

  // 不存在的key
  for(size_t i = 0; i < count; ++i) {
    EXPECT_EQ(adapter.CommentRemove(1000 + i, article_id, sender_id), 0);
    EXPECT_EQ(adapter.CommentCountByArticle(1000 + i), 0);
  }

}

int main(int argc, char* argv[]) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

